﻿using System;
using System.Collections.Generic;
using Resources.Scripts.Socket.MyChat.Manager;
using Resources.Scripts.Socket.MyChat.Protocol;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Resources.Scripts.Socket.MyChat.Client
{
    public class UIChat : MonoBehaviour
    {
        // private TMP_Text textArea;
        // private TMP_InputField chatText;
        
        //  聊天内容区域
        private RectTransform content;
        //  单条文本存放对象
        private GameObject template;
        //  即将要发送的消息
        private TMP_InputField chatText;

        private List<Toggle> channels;
        private Button send;

        private void Start()
        {
            channels = new List<Toggle>();
            var side = transform.Find("Bg/Side");
            for (int i = 0; i < side.childCount; i++)
            {
                channels.Add(side.GetChild(i).GetComponent<Toggle>());
                channels[i].onValueChanged.AddListener((value) =>
                {
                    var index = new int();
                    
                    index = i;
                    if (value)
                    {
                        ChatManager.Instance.SendChannel = (ChatChannel)index;
                    }
                    RefreshUI(null);
                });
            }

            content = transform.Find("Bg/Content/Scroll View/Viewport/Content").GetComponent<RectTransform>();
            template = UnityEngine.Resources.Load<GameObject>("Prefabs/Message/OneMessage");
            chatText = transform.Find("Bg/Dialog/Content").GetComponent<TMP_InputField>();
            
            transform.Find("Bg/Dialog/Send").GetComponent<Button>().onClick.AddListener(() =>
            {
                if (!chatText.text.Equals(""))
                {
                    SendChat(chatText.text);
                }
            });

            ChatManager.Instance.OnChat += RefreshUI;
        }

        private void Update()
        {
            
        }

        private void OnDestroy()
        {
            ChatManager.Instance.OnChat -= RefreshUI;
        }

        public void RefreshUI(ChatMessage message)
        {
            
            // textArea.text = ChatManager.Instance.GetCurrentMessage();

            //  直接加入content
            GameObject go = Instantiate(template, content);
            go.GetComponent<OneMessage>().Init(message);


            //  TODO: 对接收的消息进行封装

            // channelSelect.value = (int)ChatManager.Instance.sendChannel - 1;
            // if(ChatManager.Instance.sendChannel == Skill)
        }


        // public void OnClickChatLink(TMP_Text text, );

        private void SendChat(string content)
        {
            ChatManager.Instance.SendChat(content);
            GameObject go = Instantiate(template, this.content);
            go.GetComponent<OneMessage>().Init(NetClient.Instance.userId, NetClient.Instance.userName, content);
        }


        public void OnSendChannelChanged(int idx)
        {
            
        }
        
        
    }
}